package P3;

import java.util.Random;
import jmetal.base.Problem;
import jmetal.base.SolutionType;
import jmetal.base.Variable;
import TeamPursuit.MensTeamPursuit;
import TeamPursuit.SimulationResult;

public class MenCycleSolutionType extends SolutionType{

	
	public MenCycleSolutionType(Problem problem) {
		super(problem);
		super.typename = "Men";
	}

	@Override
	public Variable[] createVariables() throws ClassNotFoundException {
		//boolean [] strategy1 = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, true};
		//int [] power1 = {809, 550, 669, 550, 365, 658, 273, 911, 550, 550, 686, 550, 550, 324, 570, 550, 550, 575, 619, 598, 550, 1077, 550, 710, 550, 698, 550, 1030, 550, 550, 1139};
        
		Variable[] iniVarible=new Variable[1];
		Variable v;
		Random r = new Random();
		SimulationResult result = null;
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
		int sLength=30;
		int pLength=31;
		boolean [] strategy = new boolean[sLength];
		for(int i=0;i<sLength;i++){
			strategy[i] = r.nextBoolean();
			if(i>1&&strategy[i]==strategy[i-1]&&strategy[i]==strategy[i-2]){
				if(strategy[i]){
					strategy[i]=false;
				}else{
					strategy[i]=true;
				}
			}
		}
		int [] power = new int[pLength];
	    for(int i=0;i<pLength;i++){
			power[i] = r.nextInt(601)+200;
			//power[i] = r.nextInt(301)+300;
		}
	    try {
			result=mensTeamPursuit.simulate(strategy, power);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		while(!isValid(result)){
			for(int i=0;i<sLength;i++){
				strategy[i] = r.nextBoolean();
			}
			for(int i=0;i<pLength;i++){
				power[i] = r.nextInt(601)+200;
				//power[i] = r.nextInt(301)+300;
			}
			try {
				result=mensTeamPursuit.simulate(strategy, power);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		v=new Strategy(strategy,power);
		iniVarible[0]=v;
		return iniVarible;
	}
	
	private boolean isValid(SimulationResult result){
		if(result.getProportionCompleted() < 0.9999998){
			return false;
		}else{
			return true;
		}
	}
}
